<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>
<body>
    
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 这个 spacer 用来占位 -->
        <span class="spacer"></span>
        <!-- 来几个按钮 -->
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>

    <!-- 页面主体 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <!-- 用这个 .card 来表示用户的信息 -->
            <div class="card">
                <img src="img/2.jpg" alt="">
                <h3></h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>3</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <!-- 用这个表示一篇博客 -->
            <!-- <div class="blog">
                <div class="title">这是第一篇博客</div>
                <div class="date">2022-09-19 12:00:00</div>
                <div class="desc">从今天起我要认真敲代码. Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt amet nobis sed assumenda laboriosam nam ex cupiditate sequi ullam doloremque eius corporis, veritatis est ipsa nesciunt eveniet. Reprehenderit, error consequatur.</div>
                <a href="#">查看全文 &gt;&gt; </a>
            </div> -->
            
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <script src="js/app.js"></script>
    
    <script>

        function getBlogs() {
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body) {
                // 根据返回的 json 数据, 来构造出页面内容, div.blog
                // jquery ajax 会自动的把响应得到的 body 按照响应的 Content-Type 进行转换格式. 
                // 如果响应的 Content-Type 是 json, 此时就会自动把 body 转成 js 的对象
                let container = document.querySelector('.container-right');
                for (let blog of body) {
                    let blogDiv = document.createElement('div');
                    blogDiv.className = 'blog';
                    // 创建博客标题
                    let titleDiv = document.createElement('div');
                    titleDiv.className = 'title';
                    titleDiv.innerHTML = blog.title;
                    blogDiv.appendChild(titleDiv);
                    // 创建日期
                    let dateDiv = document.createElement('div');
                    dateDiv.className = 'date';
                    dateDiv.innerHTML = blog.postTime;
                    blogDiv.appendChild(dateDiv);
                    // 创建摘要
                    let descDiv = document.createElement('div');
                    descDiv.className = 'desc';
                    descDiv.innerHTML = blog.content;
                    blogDiv.appendChild(descDiv);
                    // 创建查看全文按钮
                    let a = document.createElement('a');
                    a.innerHTML = '查看全文 >>';
                    a.href = 'blog_detail.html?blogId=' + blog.blogId;
                    blogDiv.appendChild(a);
                    // 把 blogDiv 加入外层元素
                    container.appendChild(blogDiv);
                }
            }
        });
    }
    getBlogs();


    
    getLoginStatus();

    function getUserInfo() {
        $.ajax({
            type:'get',
            url:'userInfo',
            success: function(body) {
                 let h3 = document.querySelector('.container-left>.card>h3');
                 h3.innerHTML = body.username;


            }
        });
    }
    getUserInfo();

    </script>

</body>
</html>